package com.ls.fw.mongo.dao.impl;

import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.ls.fw.commons.core.utils.collections.MapUtils;
import com.ls.fw.data.bean.PageList;
import com.ls.fw.mongo.dao.DataDao;
import com.ls.fw.mongo.dao.MongoDbClient;
import com.ls.fw.mongo.helper.MongoDbHelper;
import com.ls.fw.mongo.utils.IdUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;

public class DataDaoImpl implements DataDao{
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	public static final String MOGODB_ID = "_id";
	
	private MongoDbClient mongoDbClient = null;
	
	 
	public void setMongoDbClient(MongoDbClient mongoDbClient) {
		this.mongoDbClient = mongoDbClient;
	}

	@Override
	public PageList<Map<String,Object>> query(String schmal, String table,Bson filter,int page, int pageSize,String orderField,int order){
		PageList<Map<String,Object>> list = new PageList<Map<String,Object>>(page,pageSize);
		MongoCursor<Document> cursor = null;
		MongoCollection<Document> coll = mongoDbClient.getCollection(schmal, table);
		cursor = this.mongoDbClient.findByPage(coll, filter , page, pageSize,orderField, order);
		while (cursor.hasNext()) {  
			Document doc = cursor.next();
			list.add(doc);
		} 
		long count = coll.count(filter);  
		list.setTotal(count);
		return list;
	}
	

	@Override
	public PageList<Map<String,Object>> query(String schmal,String table,Map<String,String> para,int page, int pageSize,String orderField,int order) throws Exception{
		final BasicDBObject query = new BasicDBObject();  
		MapUtils.traversal(para, new MapUtils.KeyValueMapper<String, String>(){

			@Override
			public boolean next(String arg0, String arg1, int arg2) throws Exception {
				//完全匹配
				//Pattern pattern = Pattern.compile("^name$", Pattern.CASE_INSENSITIVE);
				//右匹配
				//Pattern pattern = Pattern.compile("^.*name$", Pattern.CASE_INSENSITIVE);
				//左匹配
				//Pattern pattern = Pattern.compile("^name.*$", Pattern.CASE_INSENSITIVE);
				//模糊匹配
			    Pattern pattern = Pattern.compile("^.*"+arg1+".*$", Pattern.CASE_INSENSITIVE);
			    query.put(arg0,pattern);
				return true;
			}
		});
		return query(schmal, table, query ,page, pageSize, orderField, order);
	}

	
	@Override
	public void empty(String schmal,String table) {
		this.mongoDbClient.dropCollection(schmal, table);
	}
	
	@Override
	public void add(String schmal,String table,Map<String,Object> map) throws Exception{
		String id = null;
		if(map.containsKey(MongoDbClient._ID)){
			id = (String) map.get(MongoDbClient._ID);
		}
		if(StringUtils.isBlank(id)){
			id = IdUtils.getMD5(JSON.toJSONString(map).hashCode()+"");
			map.put(MongoDbClient._ID, id);
		}
		this.mongoDbClient.insert(schmal, table, map);
	}

	@Override
	public void update(String schmal,String table,Map<String,Object> map,String id) throws Exception{
		MongoCollection<Document> coll = mongoDbClient.getCollection(schmal, table);
		this.mongoDbClient.updateById(coll, id, MongoDbHelper.mapToDocument(map));
	}

	@Override
	public long delete(String schmal,String table,String... id){
		if(id == null || id.length<=0){
			return 0;
		}
		MongoCollection<Document> coll = mongoDbClient.getCollection(schmal, table);
		long i = this.mongoDbClient.deleteById(coll, id);
		return i;
	}


	@Override
	public void persist(String schmal,String table,Map<String, Object> model) throws Exception {
		String id = null;
		if(model.containsKey(MongoDbClient._ID)){
			id = (String) model.get(MongoDbClient._ID);
		}
		boolean add = false;
		if(StringUtils.isBlank(id)){
			add = true;
			id = IdUtils.getMD5(JSON.toJSONString(model).hashCode()+"");
			model.put(MongoDbClient._ID, id);
		}
		if(add){
			this.add(schmal,table, model);
		}else{
			this.update(schmal,table, model,id);
		}
	}


	@Override
	public Map<String, Object> find(String schmal,String table,String  id) throws Exception {
		MongoCollection<Document> coll = mongoDbClient.getCollection(schmal, table);
		return this.mongoDbClient.findById(coll, id);
	}


	@Override
	public MongoDbClient getMongoDbClient() {
		return mongoDbClient;
	}
}
